package 二刷热题100;

import java.util.HashMap;
import java.util.Map;

public class p1两数之和 {
    public int[] twoSum(int[] nums, int target) {
        //利用 哈希表
        Map<Integer,Integer> map=new HashMap<>();
        int[] res=new int[2];
        for (int i = 0; i <nums.length; i++) {
            if (map.containsKey(target-nums[i])) {
                res[0]=map.get(target-nums[i]);
                res[1]=i;
            }
            map.put(nums[i],i);
        }
        return res;


    }


    public int[] twoSum1(int[] nums, int target) {
        //暴力枚举
        int[] res=new int[2];
        for (int i = 0; i <nums.length; i++) {
            for (int j = i+1; j <nums.length; j++) {
                if (nums[i]+nums[j]==target) {
                    res[0]=i;
                    res[1]=j;
                    return res;
                }
            }
        }
        return res;
    }
}
